package util.parser;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;

import util.io.FileInput;

public class DBPedia {

	
	
	
	
	public static String[] parseWikiTitleEntry(String line){
		
		
		
		line = line.replace("<http://dbpedia.org/resource/", "");
		
		
		int i = line.indexOf(">");
		String t[] = new String[2];
		t[0]= line.trim().substring(0,i);
		
		line = line.substring(i,line.length());
		 
		
		
		line = line.replace("> <http://www.w3.org/2000/01/rdf-schema#label> \"", "");
		
		i = line.indexOf("\"");
		t[1]= line.substring(0,i);
		
		
			return t;
			
	}

	
	public static LinkedList<String> parseDBpediaResourceEntry(String line){
		
		
		
		String t[] = line.split("\\s+");
		LinkedList<String> list = new LinkedList<String>();
		
			
		
		for(int i =0 ; i < t.length; i++){
			
			
			if(t[i].toLowerCase().startsWith("<http://dbpedia.org/resource/")){
				
				String temp = t[i].replace("<http://dbpedia.org/resource/", "");
				temp   = temp.replace(">", "");
				list.add(temp);
				
			}
		}
		
				
		
			return list;
			
	}

	
	
	/**
	 * From entries of the article_categories dbpedia file
	 * 
	 * @param f
	 * @param filter
	 */
	public static HashSet<String> extractCategories(String f, String filter) {

		FileInput file = new FileInput(f);
		HashSet<String> set = new HashSet<String>();

		String line = file.readString();
		int n = 0;
		while (line != null) {

			String t[] = line.split("\\s+");

			for (int i = 0; i < t.length; i++) {

				if (t[i].toLowerCase().startsWith(
						"<http://dbpedia.org/resource/categ")) {
					n++;

					
					if(t[i].contains(filter)){
					String temp = t[i].replace("<http://dbpedia.org/resource/", "");
					
					
					temp = temp.replaceAll(">","");
					set.add(temp);

					
					}
				}

			}

			line = file.readString();
		}

		
		Iterator<String> iter = set.iterator();
		
		n=0;
		while(iter.hasNext()){
			
			String t=  iter.next();
			System.out.println(t);
			
			n++;
		}
		System.out.println(n);
		return set;
		
	}

	
	
	
	public static Hashtable<String, LinkedList<String>> getWikisOfCategories(String targetCategoriesFile, String filter,  
			String dbpediaFile) {
		
		
		
		
		HashSet<String> categories = extractCategories(targetCategoriesFile, filter);

		Hashtable<String, LinkedList<String>> wikis = getWikisOfCategories(categories, dbpediaFile);
		
		
		
		printHash(wikis);
		
		
		return wikis;

	}
	
	
	
	/**
	 * 
	 * 
	 * @param dbpedia
	 * @return
	 */
	public static  Hashtable<String, LinkedList<String>> getWikisOfCategories(String dbpedia) {

		Hashtable<String, LinkedList<String>> wikis = new Hashtable<String, LinkedList<String>>();

		FileInput in = new FileInput(dbpedia);

		String line = in.readString();

		while (line != null) {

			LinkedList<String> entry = parseDBpediaResourceEntry(line);
			
			// first position wikipedia and then the categories

			String wiki = entry.remove(0);
			
		
			updateHash(wikis,entry,wiki);
			line= in.readString();

		}

		return wikis;

	}
	
	
	/**Get files with dbpedia entries
	 * 
	 * Output file in the form category		Wiki1	Wiki2	
	 * 
	 * @param categoriesFile
	 */
	public static  Hashtable<String, LinkedList<String>> getWikisOfCategories(
			HashSet<String> targetCategories, String dbpedia) {

		Hashtable<String, LinkedList<String>> wikis = new Hashtable<String, LinkedList<String>>();

		FileInput in = new FileInput(dbpedia);

		String line = in.readString();

		while (line != null) {

			LinkedList<String> entry = parseDBpediaResourceEntry(line);
			
			// first position wikipedia and then the categories

			String wiki = entry.remove(0);

			// if there is an intersection
			if (targetCategories.contains(entry)) {
				updateHash(wikis, entry,wiki);
				
			}
			
			
			
			line =in.readString();

		}

		return wikis;

	}
	
	
	public static void printHash(Hashtable<String, LinkedList<String>> hash) {

		Enumeration<String> keys = hash.keys();

		while (keys.hasMoreElements()) {

			String key = keys.nextElement();
			
			LinkedList<String> list = hash.get(key);
			System.out.print(key+ "\t"); 
			for(int i =0; i < list.size(); i++){
				System.out.print(list.get(i)+ "\t");
				
			}
			System.out.println("");
		}

	}

	/**
	 * Insert in hash  entrys in the form <entry(0),wiki>, <entry(1),wiki>...
	 * 
	 * 
	 * @param wikis
	 * @param entry
	 * @param wiki
	 */
	private static void updateHash(Hashtable<String, LinkedList<String>> wikis,
			LinkedList<String> entry, String wiki) {
		// TODO Auto-generated method stub

		for (int i = 0; i < entry.size(); i++) {

			if (wikis.containsKey(entry.get(i))) {

				wikis.get(entry.get(i)).add(wiki);

			} else {

				LinkedList<String> list = new LinkedList<String>();

				list.add(wiki);

				wikis.put(entry.get(i), list);

			}

		}

	}


	public static void main(String args[]){
		
		
		String a="<http://dbpedia.org/resource/AfghanistanHistory> <http://www.w3.org/2000/01/rdf-schema#label> \"AfghanistanHistory\"@en <http://en.wikipedia.org/wiki/AfghanistanHistory#> .";
		
		
		String file = "/media/sata/data/dbpedia/games.txt";
		
		
		String dbpedia = "/media/sata/data/dbpedia/article_categories_en.nq";
		
		getWikisOfCategories(file,"games",dbpedia);
		
	//	String t[] = parseWikiTitleEntry(a);
		
		//System.out.println(t[0] + "\t" + t[1]);
		
		
	}
	
	
	
	
	
	
	
	
}
